﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TKSTAGZUO
    {

        public TKSTAGZUO()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKSTAGZUO();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();

            dwData["KSZMANDANT"] = app.Mandant;
            dwData["KSZKSTKRS"] = " AND KSZKSTKRS LIKE '" + KRS + "%'";
            dwData["KSZKST"] = " AND KSZKST LIKE '" + LFD + "%'";
            SResult = TMV.QKSZ02(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {

                if (!IsNew)
                {
                    IsNew = true;
                    POS = "";
                    return;
                }
                if (POS.Trim().Length < 2)
                {
                    throw new Exception("必须先输入操作类型"); 
                }
                filter = "KSZAGANG='" + POS.PadRight(15) + "'";
                rows = TBMain.Select(filter);
                if(rows.Length>0)
                    throw new Exception("重复了");
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["KSZMANDANT"] = app.Mandant;
                dwData["KSZKSTKRS"] = KRS;
                dwData["KSZKST"] = LFD;
                dwData["KSZAGANG"] = POS;
                TMV.Insert(dwData);
                vwMaster.View.MoveCurrentToFirst();
                IsNew = false;
                POS = "";
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {

            }
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DKSZ01(dwMaster);
            filter = "KSZKSTKRS='" + dwMaster["KSZKSTKRS"].ToString() + "' AND KSZKST='" + dwMaster["KSZKST"].ToString() + "' AND KSZAGANG='" + dwMaster["KSZAGANG"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {

                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        } 
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 6)
                return;
            dwData = TMV.TBMain.NewRow();
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 4);
            dwData["KSZMANDANT"] = app.Mandant;
            dwData["KSZKSTKRS"] = " AND KSZKSTKRS='" + EXAUF.Substring(0, 2) + "'";
            dwData["KSZKST"] = " AND KSZKST='" + EXAUF.Substring(2, 4) + "'";
            SResult = TMV.QKSZ02(dwData);
            MergeData(SResult, false);
            base.OnEXAUFChg(vlaue);
        }
    }
}
